package Day2025_03_04;

public class LeetCode43 {
    public String multiply(String num1, String num2) {
        int m = num1.length(), n = num2.length();
        char[] nums1 = new StringBuffer(num1).reverse().toString().toCharArray();
        char[] nums2 = new StringBuffer(num2).reverse().toString().toCharArray();
        int[] tmp = new int[m + n - 1];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                tmp[i + j] += (nums2[i] - '0') * (nums1[j] - '0');
            }
        }
        // 处理进位
        int cur = 0, t = 0;
        StringBuilder ret = new StringBuilder();
        while (cur < m + n - 1 || t != 0) {
            if (cur < m + n - 1) t += tmp[cur];
            ret.append((char) (t % 10 + '0'));
            cur++;
            t /= 10;
        }
        while (ret.length() > 1 && ret.charAt(ret.length() - 1) == '0') {
            ret.deleteCharAt(ret.length() - 1);
        }
        return ret.reverse().toString();
    }
}
